package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult<Clazz> getPageResult(ClazzQueryParam param) {
        try {
            Page<Clazz> page = new Page<>(param.getPage(), param.getPageSize());
            page.addOrder(OrderItem.desc("update_time"));
            IPage<Clazz> resultPage = clazzMapper.page(page); // 确保调用正确的 Mapper 方法
            return new PageResult<>(resultPage.getTotal(), resultPage.getRecords());
        } catch (Exception e) {
            // 记录异常日志
            e.printStackTrace();
            // 可以选择抛出异常或返回默认值
            throw new RuntimeException("分页查询失败", e);
        }
    }
}
